package com.atguigu.springbootdemo.controller;

import com.atguigu.springbootdemo.bean.Employee;
import com.atguigu.springbootdemo.service.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Smexy on 2022/11/15
 */
@RestController
public class EmployeeController
{

    @Autowired
    private EmployeeServiceImpl employeeService;

    @RequestMapping(value = "/emp")
   public Object handle1(String op,Integer id,String lastname,String gender,String email){

        //封装数据模型
        Employee emp = new Employee(id, lastname, gender, email);

        switch (op){
            case "select":  if (id == null){
                return "id非法!";
            }else{
                //调用service
                Employee e = employeeService.getById(id);
                //调用Mapper
                //employeeService.getBaseMapper().selectById(id);

                return e == null ? "查无此人" : e;
            }

            case "delete": if (id == null){
                return "id非法!";
            }else{
                employeeService.removeById(id);
                return "success";
            }

            case "insert": employeeService.save(emp);
                    return "success";

            case "update": if (id == null){
                return "id非法!";
            }else{
                employeeService.saveOrUpdate(emp);
                return "success";
            }

            default :  return "success";
        }

   }


    @RequestMapping(value = "/getAllEmp")
    public Object handle2(){
        List<Employee> all = employeeService.list();

        return all;
    }

}
